//
//  HMStatusYuanChuangView.swift
//  微博
//
//  Created by 程双岐 on 16/9/23.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import SDWebImage

class HMStatusYuanChuangView: UIView {
    
    var yuanChuanViewBottomConstraint: Constraint?
    
    var statusViewModel : HMStadusViewModel?{
        didSet{
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            if let userImageUrl =  statusViewModel?.status?.user?.profile_image_url{
                
                userImageView.sd_setImage(with: URL(string: userImageUrl))
            }
            
            TimeLabel.text = statusViewModel?.status?.created_at
            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedString!
            
            mbrankImageView.image = statusViewModel?.mbrankimage
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            sourceNameLabel.text = statusViewModel?.sourceContent
            TimeLabel.text = statusViewModel?.timeContent
            
            yuanChuanViewBottomConstraint?.uninstall()
       
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0{
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                   yuanChuanViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHHomeTableViewCellMargin).constraint
                })
                
                pictureView.picUrls = picUrls
            }else{
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                  yuanChuanViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HMHHomeTableViewCellMargin).constraint
                })
            }
            
            
        }
    }
    

    //用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named : "avatar_default_big"))
    //认证等级
    
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named : "avatar_vip"))
    //原创微博
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGray, fontSize: 14)

    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named : "common_icon_membership"))
    //发布时间
    private lazy var TimeLabel: UILabel = UILabel(textColor: UIColor.orange, fontSize: 11)

    private lazy var sourceNameLabel: UILabel = UILabel(textColor: UIColor.darkGray, fontSize: 11)

    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var pictureView: HMStatusPictureView = {
        let view = HMStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
        setupUI()
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(TimeLabel)
        addSubview(sourceNameLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        userImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(HMHHomeTableViewCellMargin)
            make.leading.equalTo(self).offset(HMHHomeTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(HMHHomeTableViewCellMargin)
        }
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HMHHomeTableViewCellMargin)
        }
        TimeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView.snp_trailing).offset(HMHHomeTableViewCellMargin)
            make.bottom.equalTo(userImageView)
        }
        sourceNameLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(TimeLabel.snp_trailing).offset(HMHHomeTableViewCellMargin)
            make.bottom.equalTo(TimeLabel)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView.snp_bottom).offset(HMHHomeTableViewCellMargin)
            make.leading.equalTo(userImageView)
            make.width.equalTo(ScrenWidth - 2 * HMHHomeTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(HMHHomeTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) in
          make.bottom.equalTo(pictureView.snp_bottom).offset(HMHHomeTableViewCellMargin)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
    }

}
